import java.util.*;

/**
 * @author LKQ
 * @date 2022/4/13 14:34
 * @description 区间S(i,j)之间的和为 S(i, j) = preSum[j + 1] - preSum[i], 注意要用long保存前缀和
 *
 */
public class Solution {
    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] nums = {-2, 5, -1};
        solution.countRangeSum(nums, -2, 2);
    }
    public int countRangeSum(int[] nums, int lower, int upper) {
        int n = nums.length;
        long[] preSum = new long[n + 1];
        for (int i = 0; i < n; i++) {
            preSum[i + 1] = preSum[i] + nums[i];
        }
        int ret = 0;
        for (int i = 0; i < n; i++) {
            for (int j = i; j < n; j++) {
                long sum = preSum[j + 1] - preSum[i];
                if (sum >= lower && sum <= upper) {
                    ret++;
                }
            }
        }
        return ret;
    }
}
